﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//using DatabaseModule; used by emc only
//  <value>Data Source=sauna\sauna;Initial Catalog=Sauna;User Id=sa;Password=pass;</value>
using EMC;

namespace ServerNetwork
{
    public class Protocol
    {
        public static String ParseData(String mstrMessage)
        {
            String mstrResponse = String.Empty;
            String protocol = String.Empty;
            String message = String.Empty;
            int messageSize = 0;
            UserManagement user = new UserManagement();

            mstrMessage.Split(':');
            protocol = mstrMessage.Split(':')[0];
            messageSize = Convert.ToInt32(mstrMessage.Split(':')[1]);
            message = mstrMessage.Split(':')[2].Substring(0, messageSize);

            switch (protocol)
            {
                case "Login":

                    try
                    {
                        String username = message.Split('@')[0];
                        String password = message.Split('@')[1];

                        if (user.userExists(message.Split('@')[0]))
                        {
                            mstrResponse = "1";
                        }
                        else
                        {
                            mstrResponse = "0";
                        }
                    }
                    catch { Exception e; mstrResponse = "0"; }

                    break;

                case "GetUser":

                    Dictionary<string, string> fieldVals = new Dictionary<string, string>();
                    fieldVals = user.getUser(message);

                    break;

                case "CreateUser":

                    try
                    {
                        String username = message.Split('@')[0];
                        String firstname = message.Split('@')[1];
                        String lastname = message.Split('@')[2];
                        String password = message.Split('@')[3];

                        //user.adduser(un, fn, ln, pw);
                        user.adduser(username, firstname, lastname, password);
                    }
                    catch { Exception e; mstrResponse = "0"; }
                    finally { mstrResponse = "1"; }

                    break;
            }

            return mstrResponse;
        }

        public static void StartServer()
        {
            Network network = new Network();
            network.createListener();
        }
    }
}
